function mapChildren(children, func, context) {
    if (!children) {
        return children;
    }
    let source = [], middle = [], result = [];
    if (Array.isArray(children)) { // children如果是数组 展开数组
        source = [...children]
    } else { 
        source = [children]
    }
    let prefix = '';
    const traverseChildren = (accumulator, current, index) => {
        if(Array.isArray(current)) { // current是数据递归执行reducer
            prefix = `${index}:`
            current.reduce(traverseChildren, accumulator);
            prefix = '';
        } else {
            current.key = `.${prefix}${current.key ? `$${current.key}` : index}`
            accumulator.push(current);
        }
        return accumulator;
    }
    middle = source.reduce(traverseChildren, middle);
    if (func && typeof func === 'function') {
        const setKey = (item, child, index) => {
            child.key = `${item.key}/.${child.key ? child.key : index}`;
            return child;
        }
        middle.map((item, index) => {
           const childs = func(item, index);
           if (Array.isArray(childs)) {
                childs.map(child => result.push(setKey(item, child, index)));
           } else {
               result.push(setKey(item, childs, index));
           }
        });
    } else {
        result = middle;
    }
    return result;
}

export {
    mapChildren as map,
};